package cn.com.haopy.yue.ai.db.service.hos.impl;


import cn.com.haopy.yue.ai.db.entity.arisk.HOSPermission;
import cn.com.haopy.yue.ai.db.mapper.arisk.HOSPermissionMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.HOSPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class HOSPermissionServiceImpl extends BaseServiceImpl<HOSPermissionMapper, HOSPermission> implements HOSPermissionService {
    @Override
    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<String>();
        if (roleIds.length == 0) {
            return permissions;
        }

        LambdaQueryWrapper<HOSPermission> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(HOSPermission::getRoleId, roleIds);
        List<HOSPermission> permissionList = super.list(queryWrapper);

        for (HOSPermission permission : permissionList) {
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    @Override
    public Set<String> queryByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if (roleId == null) {
            return permissions;
        }

        LambdaQueryWrapper<HOSPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HOSPermission::getRoleId, roleId);
        List<HOSPermission> permissionList = super.list(queryWrapper);

        for (HOSPermission permission : permissionList) {
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    @Override
    public boolean checkSuperPermission(Integer roleId) {
        if (roleId == null) {
            return false;
        }

        LambdaQueryWrapper<HOSPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HOSPermission::getRoleId, roleId)
                .eq(HOSPermission::getPermission, "*");

        return super.count(queryWrapper) != 0;
    }

    @Override
    public boolean deleteByRoleId(Integer roleId) {
        LambdaQueryWrapper<HOSPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HOSPermission::getRoleId, roleId);
        return super.remove(queryWrapper);
    }
}
